package com.zhouheng.common.base.enums;

import com.zhouheng.common.base.exception.SysErrorException;
import com.zhouheng.common.base.enums.BaseEnum;

import java.util.Arrays;
import java.util.Optional;

public class EnumUtils {
    public static <T, R extends BaseEnum<T>> R getEnumByValue(Class<R> enumType, T value) {
        Optional<R> optional = BaseEnum.valueOf(enumType, value);
        return (R) optional.orElseThrow(() -> new SysErrorException(SysErrorException.ERROR));
    }
    
    public static <T extends BaseEnum> boolean isEnumName(Class<T> enumType, String name) {
        if (name == null) {
            return false;
        }
        return Arrays.stream(enumType.getEnumConstants()).filter(v -> v.toString().equals(name.trim().toUpperCase())).findFirst().isPresent();
    }
}
